package com.navi.loaddata.loadDao;

import com.navi.loaddata.core.errorLog.ErrorCode;
import com.navi.loaddata.core.etlException.ETLException2;
import com.navi.loaddata.core.mysqlEntity.EtlConfDModel;
import com.navi.loaddata.core.oracleEntity.*;
import com.navi.loaddata.core.repository.impl.OracleRepositoryImpl;
import com.navi.loaddata.core.util.LoggerUtil;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author xufan
 */
@Service
@Transactional(transactionManager = "thirdaryPlatformTransactionManager",
        rollbackFor = {RuntimeException.class, Exception.class})
public class SavePdsDataDao {
    private static final Logger logger = LoggerFactory.getLogger(SavePdsDataDao.class);
    private LoggerUtil loggerUtil = new LoggerUtil(this.getClass());

    @Autowired
    private OracleRepositoryImpl dao3;

    public void savePdsDataP(EtlConfDModel etlConfD, EtlPdsGlassV etlPdsGlassV, List<EtlPdsGlassSummaryV> etlPdsGlassSummaryVList, List<EtlPdsResultV> etlPdsResultVList) throws Exception {
        Session session = dao3.getEntityManagerSession();
        String SQLFileProcedure = "";
        try {
            switch (etlConfD.getShop()) {
                case "ARRAY":
                    SQLFileProcedure = "{Call ARRAY_PDS_LOADER.LOAD_PDS()}";
                    dao3.save(EtlPdsGlassVMapper.INSTANCE.etlGlassToArray(etlPdsGlassV));
                    if(!etlPdsResultVList.isEmpty()) dao3.saveAll(EtlPdsResultVMapper.INSTANCE.etlGlassToArray(etlPdsResultVList));
                    if(!etlPdsGlassSummaryVList.isEmpty()) dao3.saveAll(EtlPdsGlassSummaryVMapper.INSTANCE.etlGlassToArray(etlPdsGlassSummaryVList));
                    break;
                case "CF":
                    SQLFileProcedure = "{Call CF_PDS_LOADER.LOAD_PDS()}";
                    dao3.save(EtlPdsGlassVMapper.INSTANCE.etlGlassToCf(etlPdsGlassV));
                    if(!etlPdsResultVList.isEmpty()) dao3.saveAll(EtlPdsResultVMapper.INSTANCE.etlGlassToCf(etlPdsResultVList));
                    if(!etlPdsGlassSummaryVList.isEmpty()) dao3.saveAll(EtlPdsGlassSummaryVMapper.INSTANCE.etlGlassToCf(etlPdsGlassSummaryVList));

                    break;
                case "OC":
                    if ("CELL1".equals(etlConfD.getCellType())) {
                        SQLFileProcedure = "{Call CELL1_PDS_LOADER.LOAD_PDS()}";
                        dao3.save(EtlPdsGlassVMapper.INSTANCE.etlGlassToCell1(etlPdsGlassV));
                        if(!etlPdsResultVList.isEmpty()) dao3.saveAll(EtlPdsResultVMapper.INSTANCE.etlGlassToCell1(etlPdsResultVList));
                        if(!etlPdsGlassSummaryVList.isEmpty()) dao3.saveAll(EtlPdsGlassSummaryVMapper.INSTANCE.etlGlassToCell1(etlPdsGlassSummaryVList));

                    } else if ("CELL2".equals(etlConfD.getCellType())) {
                        SQLFileProcedure = "{Call CELL2_PDS_LOADER.LOAD_PDS()}";
                        dao3.save(EtlPdsGlassVMapper.INSTANCE.etlGlassToCell2(etlPdsGlassV));
                        if(!etlPdsResultVList.isEmpty()) dao3.saveAll(EtlPdsResultVMapper.INSTANCE.etlGlassToCell2(etlPdsResultVList));
                        if(!etlPdsGlassSummaryVList.isEmpty()) dao3.saveAll(EtlPdsGlassSummaryVMapper.INSTANCE.etlGlassToCell2(etlPdsGlassSummaryVList));
                    }
                    break;
            }
        } catch (Exception e) {
            throw new ETLException2(ErrorCode.ERROR_SAVE_ORA_PDS_GLASS,e);
        }


//         call proc
        try {
            SQLQuery queryDefect = session.createSQLQuery(SQLFileProcedure);
            queryDefect.executeUpdate();
        } catch (Exception e) {
            throw new ETLException2(ErrorCode.ERROR_PROCEDURE_ORA_PDS_GLASS,e);
        }

    }

    public void savePdsDataI(EtlConfDModel etlConfD, EtlGlassV etlGlassV, List<EtlGlassSummaryV> etlGlassSummaryVList, List<EtlResultV> etlResultVList) throws Exception {
        Session session = dao3.getEntityManagerSession();
        String SQLFileProcedure = "";
        try {
            switch (etlConfD.getShop()) {
                case "ARRAY":
                    SQLFileProcedure = "{Call ARRAY_FILE_LOADER.LOAD_DATA()}";
                    dao3.save(EtlGlassVMapper.INSTANCE.etlGlassToArray(etlGlassV));
                    if(!etlResultVList.isEmpty()) dao3.saveAll(EtlResultVMapper.INSTANCE.etlGlassToArray(etlResultVList));
                    if(!etlGlassSummaryVList.isEmpty()) dao3.saveAll(EtlGlassSummaryVMapper.INSTANCE.etlGlassToArray(etlGlassSummaryVList));
                    break;
                case "CF":
                    SQLFileProcedure = "{Call CF_FILE_LOADER.LOAD_DATA()}";
                    dao3.save(EtlGlassVMapper.INSTANCE.etlGlassToCf(etlGlassV));
                    if(!etlResultVList.isEmpty()) dao3.saveAll(EtlResultVMapper.INSTANCE.etlGlassToCf(etlResultVList));
                    if(!etlGlassSummaryVList.isEmpty()) dao3.saveAll(EtlGlassSummaryVMapper.INSTANCE.etlGlassToCf(etlGlassSummaryVList));

                    break;
                case "OC":
                    if ("CELL1".equals(etlConfD.getCellType())) {
                        SQLFileProcedure = "{Call CELL1_FILE_LOADER.LOAD_DATA()}";
                        dao3.save(EtlGlassVMapper.INSTANCE.etlGlassToCell1(etlGlassV));
                        if(!etlResultVList.isEmpty()) dao3.saveAll(EtlResultVMapper.INSTANCE.etlGlassToCell1(etlResultVList));
                        if(!etlGlassSummaryVList.isEmpty()) dao3.saveAll(EtlGlassSummaryVMapper.INSTANCE.etlGlassToCell1(etlGlassSummaryVList));

                    } else if ("CELL2".equals(etlConfD.getCellType())) {
                        SQLFileProcedure = "{Call CELL2_FILE_LOADER.LOAD_DATA()}";
                        dao3.save(EtlGlassVMapper.INSTANCE.etlGlassToCell2(etlGlassV));
                        if(!etlResultVList.isEmpty()) dao3.saveAll(EtlResultVMapper.INSTANCE.etlGlassToCell2(etlResultVList));
                        if(!etlGlassSummaryVList.isEmpty()) dao3.saveAll(EtlGlassSummaryVMapper.INSTANCE.etlGlassToCell2(etlGlassSummaryVList));
                    }
                    break;
            }
        } catch (Exception e) {
            throw new ETLException2(ErrorCode.ERROR_SAVE_ORA_PDS_GLASS,e);
        }


//         call proc
        try {
            SQLQuery queryDefect = session.createSQLQuery(SQLFileProcedure);
            queryDefect.executeUpdate();
        } catch (Exception e) {
            throw new ETLException2(ErrorCode.ERROR_PROCEDURE_ORA_PDS_GLASS,e);
        }

    }


    public void deletePds(EtlConfDModel etlConfD) throws Exception {
        Session session = dao3.getEntityManagerSession();
        String hqlGlass = "";
        String hqlResult = "";
        String hqlSummary = "";
        String hqlPdsGlass = "";
        String hqlPdsResult = "";
        String hqlPdsSummary = "";
        try {
            switch (etlConfD.getShop()) {
                case "ARRAY":
                    hqlGlass = "Delete FROM EtlArrayGlassV";
                    hqlResult = "Delete FROM EtlArrayResultV";
                    hqlSummary = "Delete FROM EtlArrayGlassSummaryV";
                    hqlPdsGlass = "Delete FROM EtlArrayPdsGlassV";
                    hqlPdsResult = "Delete FROM EtlArrayPdsResultV";
                    hqlPdsSummary = "Delete FROM EtlArrayPdsGlassSummaryV";
                    break;
                case "CF":
                    hqlGlass = "Delete FROM EtlCfGlassV";
                    hqlResult = "Delete FROM EtlCfResultV";
                    hqlSummary = "Delete FROM EtlCfGlassSummaryV";
                    hqlPdsGlass = "Delete FROM EtlCfPdsGlassV";
                    hqlPdsResult = "Delete FROM EtlCfPdsResultV";
                    hqlPdsSummary = "Delete FROM EtlCfPdsGlassSummaryV";
                    break;
                case "OC":
                    if ("CELL1".equals(etlConfD.getCellType())) {
                        hqlGlass = "Delete FROM EtlCellComponentV";
                        hqlResult = "Delete FROM EtlCellResultV";
                        hqlSummary = "Delete FROM EtlCellComponentSummaryV";
                        hqlPdsGlass = "Delete FROM EtlCellPdsComponentV";
                        hqlPdsResult = "Delete FROM EtlCellPdsResultV";
                        hqlPdsSummary = "Delete FROM EtlCellPdsComptSummaryV";
                    } else if ("CELL2".equals(etlConfD.getCellType())) {
                        hqlGlass = "Delete FROM EtlCell2ChipV";
                        hqlResult = "Delete FROM EtlCell2ResultV";
                        hqlSummary = "Delete FROM EtlCell2ChipSummaryV";
                        hqlPdsGlass = "Delete FROM EtlCell2PdsChipV";
                        hqlPdsResult = "Delete FROM EtlCell2PdsResultV";
                        hqlPdsSummary = "Delete FROM EtlCell2PdsChipSummaryV";
                    }
                    break;
            }
            Query qGlass = session.createQuery(hqlGlass);
            Query qResult = session.createQuery(hqlResult);
            Query qSummary = session.createQuery(hqlSummary);
            Query qPdsGlass = session.createQuery(hqlPdsGlass);
            Query qPdsResult = session.createQuery(hqlPdsResult);
            Query qPdsSummary = session.createQuery(hqlPdsSummary);



            qGlass.executeUpdate();
            qResult.executeUpdate();
            qSummary.executeUpdate();
            qPdsGlass.executeUpdate();
            qPdsResult.executeUpdate();
            qPdsSummary.executeUpdate();


        } catch (Exception e) {
            throw new ETLException2(ErrorCode.ERROR_DELETE_ORA_PDS_GLASS,e);
        }
    }

}
